package com.fun_ordering.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fun_ordering.components.CodeComponents;
import com.fun_ordering.mapper.DishRatioMapper;
import com.fun_ordering.mapper.MaterialStockMapper;
import com.fun_ordering.pojo.DishRatio;
import com.fun_ordering.pojo.MaterialStock;
import com.fun_ordering.service.DishInfoService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.fun_ordering.components.PageResult;
import com.fun_ordering.mapper.DishInfoMapper;
import com.fun_ordering.pojo.DishInfo;
import com.fun_ordering.pojo.DishInfoExample;
import com.fun_ordering.pojo.DishInfoExample.Criteria;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 菜品信息表服务实现层
 * @author XiaoPi
 *
 */
@Service
public class DishInfoServiceImpl implements DishInfoService {

	@Autowired
	//菜品表
	private DishInfoMapper dishInfoMapper;
	@Autowired
	//配比表
	private DishRatioMapper dishRatioMapper;
	@Autowired
	//进菜信息表
	private MaterialStockMapper materialStockMapper;
	
	/**
	 * 查询全部
	 */
	@Override
	public List<DishInfo> findAll() {
		return dishInfoMapper.selectByExample(null);
	}

	/**
	 * 按分页查询
	 */
	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);		
		Page<DishInfo> page =  (Page<DishInfo>) dishInfoMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
	}

	/**
	 * 增加
	 * 菜品添加时需要添加原料配比，
	 * 在菜品信息表里添加菜品信息，
	 * 在菜品配方表里面也要添加对应的信息
	 * ，对应两个service
	 */
	@Override
	public void add(DishInfo dishInfo) {
			//将当前菜品信息进行添加
		dishInfoMapper.insert(dishInfo);
	}

	
	/**
	 * 修改
	 */
	@Override
	public void update(DishInfo dishInfo){
		dishInfoMapper.updateByPrimaryKey(dishInfo);
	}	
	
	/**
	 * 根据ID获取实体
	 * @param dishId
	 * @return
	 */
	@Override
	public DishInfo findOne(Integer dishId){
		List<DishRatio> dishRatios = dishRatioMapper.selectByDishId(dishId);
		for (DishRatio dishRatio : dishRatios) {
//			System.out.println(dishRatio);
			String stockName = materialStockMapper.selectByPrimaryKey(dishRatio.getMaterialId()).getStockName();
			dishRatio.setMaterialName(stockName);
//			System.out.println(dishRatio);
		}
		DishInfo dishInfo = dishInfoMapper.selectByPrimaryKey(dishId);
		dishInfo.setDishRatioList(dishRatios);
		System.out.println(dishInfo);
		return dishInfo;
	}

	/**
	 * 批量删除
	 */
	@Override
	public void delete(Integer[] dishIds) {
		for (Integer dishId : dishIds) {
			dishInfoMapper.deleteByPrimaryKey(dishId);
		}
	}
	
	
	@Override
	public PageResult findPage(DishInfo dishInfo, int pageNum, int pageSize) {
		//分页
		PageHelper.startPage(pageNum, pageSize);
			DishInfoExample dishInfoExample = new DishInfoExample();
			Criteria criteria = dishInfoExample.createCriteria();
			criteria.andIsDeleteEqualTo( CodeComponents.DELETE_NO);
			if (dishInfo != null) {
				if (dishInfo.getDishName() != null && dishInfo.getDishName().length() > 0) {
					criteria.andDishNameLike("%" + dishInfo.getDishName() + "%");
				}
				if (dishInfo.getDishPrice() != null ) {
					criteria.andDishPriceEqualTo(dishInfo.getDishPrice() );
				}

				if (dishInfo.getActPrice() != null ) {
					criteria.andActPriceEqualTo(dishInfo.getActPrice() );
				}
				if (dishInfo.getStatus() != null ) {
					criteria.andStatusEqualTo(dishInfo.getStatus() );
				}
			}
			List<DishInfo> dishInfolist = dishInfoMapper.selectByExample(dishInfoExample);
			for (DishInfo info : dishInfolist) {
				info.setDishRatioList(dishRatioMapper.selectByDishId(info.getDishId()));
			}
			System.out.println(dishInfolist);
			PageInfo<DishInfo> pageInfo = new PageInfo<>(dishInfolist);
		return new PageResult(pageInfo.getTotal(), pageInfo.getList());
	}
	
}
